package com.onenine.app.activity;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

import com.faceunity.effect.EffectDataMgr;
import com.onenine.app.R;
import com.onenine.app.event.AppUpdateEvent;
import com.onenine.app.utils.CDataProcessor;
import com.onenine.app.utils.DataCacheHelper;
import com.onenine.app.utils.DataType;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.app.utils.SpCommonHelper;
import com.onenine.app.view.SingleSelectView;
import com.onenine.base.base.BaseEventActivity;
import com.onenine.base.utils.Toasty;
import com.onenine.base.view.SwitchButton;
import com.onenine.common.util.ActivityUtils;
import com.onenine.common.util.AppUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 设置
 */
public class SettingActivity extends BaseEventActivity implements View.OnClickListener, SwitchButton.OnCheckedChangeListener {

    private SingleSelectView msNewMsg, msClearCache, msFeedback, msScore, msUpdate, msUseProtocol, msAbout;
    private View btnLogout, mUpdateBubble;
    private SwitchButton mBtnNotify;

    public static void launchActivity(Context context, boolean bubbleHint) {
        Intent intent = new Intent(context, SettingActivity.class);
        intent.putExtra("bubbleHint", bubbleHint);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mToolbar.setCenterTitle(R.string.setting);
        btnLogout = findViewById(R.id.btn_logout);
        msNewMsg = findViewById(R.id.ssv_new_msg);
        msNewMsg.setNoArrayRightLogo();
        msNewMsg.setLeftText(R.string.new_message);
        msNewMsg.setLeftIndicateImage(R.drawable.ic_setting_ring);
        msClearCache = findViewById(R.id.ssv_clear_cache);
        msClearCache.setLeftText(R.string.clear_cache);
        msClearCache.setLeftIndicateImage(R.drawable.ic_setting_clear);
        msClearCache.setNoArrayRightLogo();
        msClearCache.setRightTextColor(R.color.c999);
        msFeedback = findViewById(R.id.ssv_feedback);
        msFeedback.setLeftText(R.string.suggestion_feedback);
        msFeedback.setLeftIndicateImage(R.drawable.ic_setting_feedback);
        msUpdate = findViewById(R.id.ssv_update);
        msUpdate.setLeftText(R.string.version_update);
        msUpdate.setLeftIndicateImage(R.drawable.ic_setting_about);
        msUseProtocol = findViewById(R.id.ssv_use_protocol);
        msUseProtocol.setLeftText(R.string.user_protocol);
        msUseProtocol.setLeftIndicateImage(R.drawable.ic_setting_protocol);
        msAbout = findViewById(R.id.ssv_about);
        msAbout.setLeftText(R.string.about);
        msAbout.setLeftIndicateImage(R.drawable.ic_setting_about);
        msAbout.setRightTextColor(R.color.c999);
        msAbout.setRightText("v" + AppUtils.getAppVersionName());
        msScore = findViewById(R.id.ssv_score);
        msScore.setLeftText(R.string.give_mark);
        msScore.setLeftIndicateImage(R.drawable.ic_setting_score);
        mUpdateBubble = findViewById(R.id.tv_bubble);
        mUpdateBubble.setBackgroundResource(RoleResLoader
                .getRoleDrawableResourceId(RoleResLoader.DynamicResName.CIRCLE_POINT_BUBBLE));
        mBtnNotify = findViewById(R.id.btn_msg_notify);
        mBtnNotify.setChecked(!SpCommonHelper.getInstance().isIgnoreMsgNotify());
        mBtnNotify.setCheckedColor(RoleResLoader
                .getRoleColorResourceId(RoleResLoader.DynamicResName.THEME_COLOR));
    }

    @Override
    protected void initListeners() {
        btnLogout.setOnClickListener(this);
        msUseProtocol.setOnClickListener(this);
        msFeedback.setOnClickListener(this);
        msAbout.setOnClickListener(this);
        msClearCache.setOnClickListener(this);
        msScore.setOnClickListener(this);
        msUpdate.setOnClickListener(this);
        mBtnNotify.setOnCheckedChangeListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        getUpdateBubble();
        getCacheSize();
        CDataProcessor.checkAppUpdate(this
                , false, false);
    }

    /**
     * 获取更新提醒状态
     */
    private void getUpdateBubble() {
        boolean bubbleHint = getIntent().getBooleanExtra("bubbleHint", false);
        if (bubbleHint) {
            mUpdateBubble.setVisibility(View.VISIBLE);
            msUpdate.setRightText(getString(R.string.find_new_version_title));
        } else {
            mUpdateBubble.setVisibility(View.GONE);
        }
    }

    /**
     * 获取缓存大小
     */
    private void getCacheSize() {
        try {
            msClearCache.setRightText(DataCacheHelper.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_logout:
                CDataProcessor.quitAppAndReLogin(SettingActivity.this, true);
                break;
            case R.id.ssv_use_protocol:
                ActivityUtils.startActivity(SettingActivity.this, UserProtocolActivity.class);
                break;
            case R.id.ssv_feedback:
                ReportSubmitActivity.launchSuggestionActivity(SettingActivity.this);
                break;
            case R.id.ssv_about:
                ActivityUtils.startActivity(SettingActivity.this, AboutActivity.class);
                break;
            case R.id.ssv_clear_cache:
                Toasty.showNormal(R.string.clear_success);
                DataCacheHelper.clearAllCache(SettingActivity.this);
                EffectDataMgr.clearEffectData();
                getCacheSize();
                break;
            case R.id.ssv_score:
                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setData(Uri.parse("market://details?id=" + getPackageName()));
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    Toasty.showNormal(R.string.no_android_market_app);
                }
                break;
            case R.id.ssv_update:
                CDataProcessor.checkAppUpdate(this, true
                        , mUpdateBubble.getVisibility() == View.VISIBLE);
                break;
        }
    }

    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        SpCommonHelper.getInstance().applyIgnoreMsgNotify(!isChecked);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AppUpdateEvent event) {
        if (event.updateStatus == DataType.APP_UPDATE) {
            mUpdateBubble.setVisibility(View.VISIBLE);
            msUpdate.setRightText(getString(R.string.find_new_version_title));
        }
    }

}
